package com.tuju.jetpackfirstdemo.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.ImageView;

/**
 * 贝塞尔曲线的测试
 * 蓝色的点是辅助点
 * @author Administrator
 *
 */
public class MyBezierView extends FrameLayout {

    private Paint mPaint;
    private Path mPath;
    private Point startPoint;
    private Point endPoint;
    private Point assistPoint;

    private Context mContext;
    public MyBezierView(Context context ) {
        this(context,null);
    }

    public MyBezierView(Context context , AttributeSet attrs )
    {
        this(context, attrs , 0); 
    }
    public MyBezierView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    ImageView tipImageView;

    private void init( Context context )
    {
        this.mContext = context;
        this.mPaint = new Paint();
        this.mPath = new Path();

        startPoint = new Point(100, 300);
        endPoint = new Point(1100, 300);
        assistPoint = new Point(600, 800);


        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStrokeWidth(5);

        //设置背景颜色，再设置背景为空，这样子是为了能一开始就draw
        //否则会发现onDraw不被调用
        setBackgroundColor(Color.WHITE);
        getBackground().setAlpha(0);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub


        mPaint.setColor(Color.BLACK);
        mPaint.setStyle( Paint.Style.STROKE );
        mPaint.setStrokeWidth( 5);

        mPath.reset();
        Log.e("MyView", "onDraw");
        mPath.moveTo(startPoint.x, startPoint.y);
            // 重要的就是这句
        mPath.quadTo(assistPoint.x, assistPoint.y, endPoint.x, endPoint.y);
           // 画路径
        canvas.drawPath(mPath, mPaint);
            // 画辅助点
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth( 20);
        canvas.drawPoint(assistPoint.x, assistPoint.y, mPaint);
        canvas.drawPoint(startPoint.x, startPoint.y, mPaint);
        canvas.drawPoint(endPoint.x, endPoint.y, mPaint);

        super.onDraw(canvas);    
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
            assistPoint.x = (int) event.getX();
            assistPoint.y = (int) event.getY();
            invalidate();
            break;

        default:
            break;
        }
        return true;
    }
}
